﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using QLHD.BUS;
using QLHD.Entity;

namespace QLHD.Ajax
{
    /// <summary>
    /// Summary description for DanhMuc
    /// </summary>
    public class DanhMuc : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            #region Query String
            string command = "";
            if (context.Request.Form["command"] != null)
                command = context.Request.Form["command"];
            else if (context.Request.QueryString["command"] != null)
                command = context.Request.QueryString["command"];

            if (context.Request.Form["command"] != null)
                command = context.Request.Form["command"];
            else if (context.Request.QueryString["command"] != null)
                command = context.Request.QueryString["command"];

            string nhanhangID = "";
            if (context.Request.Form["nhanhangID"] != null)
                nhanhangID = context.Request.Form["nhanhangID"];
            else if (context.Request.QueryString["nhanhangID"] != null)
                nhanhangID = context.Request.QueryString["nhanhangID"];

            string hinhthucquangcaoID = "";
            if (context.Request.Form["HinhThucQuangCaoID"] != null)
                hinhthucquangcaoID = context.Request.Form["HinhThucQuangCaoID"];
            else if (context.Request.QueryString["HinhThucQuangCaoID"] != null)
                hinhthucquangcaoID = context.Request.QueryString["HinhThucQuangCaoID"];

            string SanPhamID = "";
            if (context.Request.Form["SanPhamID"] != null)
                SanPhamID = context.Request.Form["SanPhamID"];
            else if (context.Request.QueryString["SanPhamID"] != null)
                SanPhamID = context.Request.QueryString["SanPhamID"];

            string donvitinh = "";
            if (context.Request.Form["donvitinhID"] != null)
                donvitinh = context.Request.Form["donvitinhID"];
            else if (context.Request.QueryString["donvitinhID"] != null)
                donvitinh = context.Request.QueryString["donvitinhID"];

            string TagID = "";
            if (context.Request.Form["TagID"] != null)
                TagID = context.Request.Form["TagID"];
            else if (context.Request.QueryString["TagID"] != null)
                TagID = context.Request.QueryString["TagID"];

            string WebsiteID = "";
            if (context.Request.Form["WebsiteID"] != null)
                WebsiteID = context.Request.Form["WebsiteID"];
            else if (context.Request.QueryString["WebsiteID"] != null)
                WebsiteID = context.Request.QueryString["WebsiteID"];

            string ChuyenMucID = "";
            if (context.Request.Form["ChuyenMucID"] != null)
                ChuyenMucID = context.Request.Form["ChuyenMucID"];
            else if (context.Request.QueryString["ChuyenMucID"] != null)
                ChuyenMucID = context.Request.QueryString["ChuyenMucID"];

            string BannerID = "";
            if (context.Request.Form["BannerID"] != null)
                BannerID = context.Request.Form["BannerID"];
            else if (context.Request.QueryString["BannerID"] != null)
                BannerID = context.Request.QueryString["BannerID"];
            #endregion

            JsonResponse responseData = new JsonResponse();

            if (!string.IsNullOrEmpty(command))
            {
                switch (command.ToLower())
                {
                    case "danhmuchopdong":
                        responseData = DanhMucHopDong_ListAll(context);
                        break;
                    case "hotensale":
                        responseData = NhanSu_ListAll(context);
                        break;
                    case "khachhang":
                        responseData = KhachHang_ListAll(context);
                        break;
                    case "nhanhang":
                        responseData = NhanHang_ListAll(context);
                        break;
                    case "nhomnganh":
                        responseData = NhomNganh_GetByNhanHang(nhanhangID.ChangeType<long>());
                        break;
                    case "hinhthucquangcao":
                        responseData = HinhThucQuangCao_ListAll();
                        break;
                    case "loaisanpham":
                        responseData = SanPham_GetByHinhThucQuangCao(hinhthucquangcaoID);
                        break;
                    case "donvitinh":
                        responseData = DonViTinh_GetBySanPham(donvitinh.ChangeType<long>());
                        break;
                    case "website":
                        responseData = Website_SelectAll();
                        break;
                    case "chuyenmuc":
                        responseData = ChuyenMuc_SelectAll();
                        break;
                    case "banner":
                        responseData = Banner_SelectAll();
                        break;
                    case "tag":
                        responseData = Tag_SelectAll();
                        break;
                    case "taikhoanadmk":
                        responseData = TaikhoanAdmk_SelectAll();
                        break;
                    case "filterdongia":
                        responseData = BaoGiaHopDong_FilterDonGia(hinhthucquangcaoID, SanPhamID, donvitinh, TagID, WebsiteID, ChuyenMucID, BannerID);
                        break;
                }
            }
            else
            {
                responseData.Success = false;
                responseData.Message = "Không tìm thấy command.";
            }
            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            context.Response.Write(JsonConvert.SerializeObject(responseData, Formatting.Indented));
        }

        private JsonResponse DanhMucHopDong_ListAll(HttpContext context)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.DanhMucHopDong_ListAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse NhanSu_ListAll(HttpContext context)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.DanhMucNhanSu_ListAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse KhachHang_ListAll(HttpContext context)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.DanhMucKhachHang_ListAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse NhanHang_ListAll(HttpContext context)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.DanhMucNhanHang_ListAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse NhomNganh_GetByNhanHang(long NhanHangID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.NhomNganh_GetByNhanHang(NhanHangID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse HinhThucQuangCao_ListAll()
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.HinhThucQuangCao_ListAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse SanPham_GetByHinhThucQuangCao(string HinhThucQuangCaoID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.SanPham_GetByHinhThucQuangCao(HinhThucQuangCaoID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse DonViTinh_GetBySanPham(long donvitinhID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.DonViTinh_GetBySanPham(donvitinhID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }
        private JsonResponse Website_SelectAll()
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.Website_SelectAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse ChuyenMuc_SelectAll()
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.ChuyenMuc_SelectAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse Banner_SelectAll()
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.Banner_SelectAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse Tag_SelectAll()
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.Tag_SelectAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse TaikhoanAdmk_SelectAll()
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.TaikhoanAdmk_SelectAll();
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        private JsonResponse BaoGiaHopDong_FilterDonGia(string hinhthucquangcaoID,string  SanPhamID,string  donvitinh,string  TagID,string  WebsiteID,string  ChuyenMucID,string  BannerID)
        {
            var jsonResponse = new JsonResponse();
            try
            {
                var lstCategory = BoBussiness.BaoGiaHopDong_FilterDonGia(hinhthucquangcaoID, SanPhamID, donvitinh, TagID, WebsiteID, ChuyenMucID, BannerID);
                jsonResponse.Success = true;
                jsonResponse.Data = lstCategory;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;
            }

            return jsonResponse;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}